//
// Created by wenyaxi on 24-10-29.
//
#include <common.h>

class Solution {
public:
    int search(vector<int> &nums, int target) {
        if (nums.empty()) { return -1; }
        if (nums.size() == 1) {
            return nums[0] == target ? 0 : -1;
        }

        int left = 0, right = nums.size() - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (nums[mid] == target) {
                return mid;
            }

            if (nums[mid] >= nums[0]) {
                if (target >= nums[0] && target < nums[mid]) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            } else {
                if (target > nums[mid] && target <= nums[nums.size() - 1]) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
        }
        return -1;
    }
};

int main(int argc, char *argv[]) {
    /*  [4,5,6,7,0,1,2]
        0
        [4,5,6,7,0,1,2]
        3
        [1]
        0
    */
    Solution s;
    vector<int> nums = {4, 5, 6, 7, 0, 1, 2};
    int target = 0;
    int result = s.search(nums, target);
    cout << result << endl;
}
